package com.carepad.action.input;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.carepad.entity.mapper.RecordItemViewMapper;
import com.carepad.util.ItemTransformer;

public class RecordItemsInputViewAction extends AbstractRecordItemsInputAction {

	private static final long serialVersionUID = 1L;

	// private List<RecordItemsGroup> menuItemList;
	Set<RecordItemViewMapper> menuItemList;
	private Map<Integer, List<ItemTransformer>> inputItemList;
	private String selectedAction;
	private String refid;

	public String execute() {
		try {
			//System.out.println("test");
			Date d1 = getSessionImplementationDate().getEnDate();
			//Date d1 = new Date();
			SimpleDateFormat df = new SimpleDateFormat("MM/dd/YYYY");
			String formattedDate = df.format(d1);

			List<RecordItemViewMapper> fullItemList = new ArrayList<RecordItemViewMapper>();
			fullItemList = recordItemsInputService
					.findRecordItemsInputListWithAnswers(getSessionPatient()
							.getHcsID(), formattedDate);

			menuItemList = new LinkedHashSet<RecordItemViewMapper>(fullItemList);
			inputItemList = new HashMap<Integer, List<ItemTransformer>>();

			Iterator<RecordItemViewMapper> itr = menuItemList.iterator();

			RecordItemViewMapper rivm;

			while (itr.hasNext()) {
				rivm = itr.next();
				inputItemList.put(rivm.getGroupId(),
						subList(rivm, fullItemList));
			}
			//inputItemList.get('0').get(0).get
			//for(int i=0;i<inputItemList.get)
			
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	
	/*
	 * private List<RecordItemViewMapper> subList(RecordItemViewMapper rivm,
	 * List<RecordItemViewMapper> list) {
	 * 
	 * List<RecordItemViewMapper> subList = new
	 * ArrayList<RecordItemViewMapper>();
	 * 
	 * for (RecordItemViewMapper i : list) { if
	 * (i.getGroupId().equals(rivm.getGroupId())) { subList.add(i); } }
	 * 
	 * return subList; }
	 */

	private List<ItemTransformer> subList(RecordItemViewMapper rivm,
			List<RecordItemViewMapper> list) {
		List<ItemTransformer> subList = new ArrayList<ItemTransformer>();

		for (RecordItemViewMapper item : list) {
			if (item.getGroupId().equals(rivm.getGroupId())) {
				subList.add(new ItemTransformer(item.getAnswerType(), item
						.getActionItem(), item.getAnswer(), item.getSelectedAnswer()));

			}
		}
		return subList;

	}

	public String showInputTab() {

		// execute();
		return SUCCESS;
	}
	
	public String viewVitalData() {
		return SUCCESS;
	}

	public Set<RecordItemViewMapper> getMenuItemList() {
		return menuItemList;
	}

	public void setMenuItemList(Set<RecordItemViewMapper> menuItemList) {
		this.menuItemList = menuItemList;
	}

	public String getSelectedAction() {
		return selectedAction;
	}

	public void setSelectedAction(String selectedAction) {
		this.selectedAction = selectedAction;
	}

	public Map<Integer, List<ItemTransformer>> getInputItemList() {
		return inputItemList;
	}

	public void setInputItemList(
			Map<Integer, List<ItemTransformer>> inputItemList) {
		this.inputItemList = inputItemList;
	}

	public String getRefid() {
		return refid;
	}

	public void setRefid(String refid) {
		this.refid = refid;
	}

}
